<?php
/**
 * Auther: Joshua Conero
 * Date: 2017/7/15 0015 12:42
 * Email: brximl@163.com
 * Name: 测试页面
 */

namespace App\Http\Controllers\Index;


use App\Http\Controllers\Controller;
use GuzzleHttp\Client;
use hyang\Net;
use hyang\SuPigeons;
use hyang\Util;

class Test extends Controller
{
    public function ActionInit($action=null){
        if(method_exists($this, $action)) $this->$action();
        else $this->php();
    }
    private function php(){phpinfo();}
    // pigeons
    protected function pigeons(){
        //$sp = SuPigeons::getInstance(config('pigeons.'.env('APP_ENV')));
        //$sp->setUser('Conero');
        //$rs = $sp->getOpenid();
        //$rs = $sp->getToken();
        //Util::println($rs);
        //Util::println($sp->get('api/app/aurora/dbconf'));
        //Util::println($sp->get('api/finance/account/Conero/sets'), rand(0, 100));

        //$e = $sp->getError();
        //Util::println(nl2br($sp->getError()));
        // $sp->getToken();

        // 远程数据请求测试
        //$remoteSp = SuPigeons::getInstance(config('pigeons.production'));
        $remoteSp = SuPigeons::getInstance(config('pigeons.local'));
        $remoteSp->setUser('Conero');
        /*
        Util::println(
            // $remoteSp->get('api/finance/account/{user}/sets')
            $remoteSp->get('api/finance/account/Conero/sets')
            , $remoteSp->getToken()
            , $remoteSp->getOpenid()
        );
        */
        $url = 'api/finance/account/Conero/sets?token='
            . $remoteSp->getToken()
            . '&conero_pid='.$remoteSp->conero_pid
        ;
        Util::println(
            // $remoteSp->get('api/finance/account/{user}/sets')
            $remoteSp->get($url)
            , $url
            , $remoteSp->getOpenid()
        );

        // https test
        // $https = Net::prepare('https://httpbin.org/headers');
        // $https->setOption('header',['X-Test'=>rand(100000, 999999)]);
        // println($https->getJsonByExec());
    }
    // 随机测试
    protected function test(){
        $url = 'http://127.0.0.1:7000/api/token/openid?access_token=%242y%2410%24umQky1LxUgbOcaxR84sl4uNZxy2qyA5gmk9XRSFZDN%2F.CVT4I7dq2&code=aurora&user=Conero';
        $net = Net::prepare($url);
        println(
            $net->getJsonByExec(),
            file_get_contents($url)
        );
        $rs = '{"msg":"openId \u83b7\u53d6\u6210\u529f","openID":"aDQzSkJlQXJGWC9KcXhLTE43S3B6VzVZY1I3UEpMS2JNc29IV3lBaGtRSTB0N015VE92ZnplTXppN1RFeFFzNlI1emNiMjhFTUNqdVliT2IrWDdkTmVlWFRz","code":200}';
        $rs = '{"openID":"aDQzSkJlQXJGWC9KcXhLTE43S3B6VzVZY1I3UEpMS2JNc29IV3lBaGtRSTB0N015VE92ZnplTXppN1RFeFFzNlI1emNiMjhFTUNqdVliT2IrWDdkTmVlWFRz"}';
        ifprintln(
            true,
            $rs,
            json_decode($url, true),
            json_last_error_msg(),
            'php-'.phpversion()
        );
    }
    // guzzle
    protected function pigeons_use_guzzle(){
        $client = new Client();
        $rs = $client->request('get', 'https://www.conero.cn/api/finance/account/Conero/sets');
        println($rs->getBody());
    }
    protected function user(){
        Util::println(
            $this->getUserInfo('user')
            ,rand(1, 999999)
        );
    }
    //csrf
    // 2017年9月20日 星期三
    // 错误：TokenMismatchException - post/get
    protected function csrf(){
        echo csrf_token();
    }
    // 网络测试程序
    protected function net(){
        // println(Net::prepare('https://www.conero.cn/api')->getJsonByExec());
//        println(Net::prepare('https://api.conero.cn/aurora/user/info')->getJsonByExec());

        //println(Net::prepare('https://api.conero.cn/')->getJsonByExec());
//        Net::setUrl('https://api.conero.cn/');
//        //Net::curl();
//        println(Net::curl());
        //$pigones = $this->getPigeons();

        $remoteSp = SuPigeons::getInstance(config('pigeons.production'));
        //println(['user_code'=>'test', 'pwsd'=>'123456', 'access_token'=>$remoteSp->access_token, 'project'=>'aurora']);
//        println($remoteSp->rawRequest('aurora/user/login', ['method'=>'post',
//            'data'=>['user_code'=>'test', 'pwsd'=>'123456', 'access_token'=>$remoteSp->access_token, 'project'=>'aurora']]));
        //println($remoteSp->rawRequest('aurora/user/info', ['method'=>'get', 'data'=>[]]));

        // get --------------**
        call_user_func(function (){
            //$net = Net::prepare('http://127.0.0.1:8194/_test/net_type');
            //$net = Net::prepare('http://www.httpbin.org/get');
            $net = Net::prepare('https://www.httpbin.org/get');
            $net->setOption('data', ['yang'=>'yang', 'su'=>'su']);
            echo $net->exec();
            echo '<br><br>';
        });

        // post -------------**
        call_user_func(function (){
            return false;
            $net = Net::prepare('http://127.0.0.1:8194/_test/net_type');
            $net->setOption('method', 'POST');
            $net->setOption('data', ['yang'=>'yang', 'su'=>'su']);
            echo $net->exec();
        });
        call_user_func(function (){
            $net = Net::prepare('http://www.httpbin.org/post');
            $net->setOption('method', 'POST');
            $net->setOption('data', ['yang'=>'yang', 'su'=>'su']);
            echo $net->exec();
        });
        call_user_func(function (){
            $net = Net::prepare('https://www.httpbin.org/post');
            $net->setOption('method', 'POST');
            $net->setOption('data', ['yang'=>'yang', 'su'=>'su']);
            echo $net->exec();
        });
    }
    // NET::curls
    public function curls(){
        println(
            Net::curls(['url'=>'https://www.httpbin.org/post', 'data'=>['yang'=>'yang', 'su'=>'su'], 'method'=>'post'])
            //, Net::curls(['url'=>'https://api.conero.cn/aurora/user/login', 'data'=>['yang'=>'yang', 'su'=>'su'], 'method'=>'post'])
            , json_decode(Net::curls(['url'=>'https://api.conero.cn/aurora/user/login', 'data'=>['yang'=>'yang', 'su'=>'su'], 'method'=>'post']), true)

        );
    }
    // 网络请求类型
    public function net_type(){
        $req = request();
        println(
            $req->getMethod()
            , $req->input()
        );
    }
    protected function base(){
        $pigeons = $this->getPigeons();
        $user = $this->getUserInfo('user');
        $pigeons->setUser($user);
        $token = $pigeons->getToken();
        
        println($token, $user);
    }
    public function session(){
        //println(session(['s1'=>random_int(1, 999999)]));
        if(!session('s1')){
            session(['s1'=> [random_int(1, 999999), date('YmdHis')]]);
        }
        $skey = $this->aurora('php_session.user_key');
        println(
            session('s1')
            , session($skey)
            , $skey
        );
    }
    public function session2(){
        $skey = $this->aurora('php_session.user_key');
        println(
            session('s1')
            , session('s_ajax')
            , isset($_SESSION[$skey])? $_SESSION[$skey]: NULL
            , session($skey)
            , $skey
        );
    }
}